﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ElemenTable
{
    public partial class ElementForm : Form
    {
        private ChemElement elem;
        private TableManager ptemanager;

        public ElementForm(TableManager ptemanager, ChemElement elem)
        {
            this.ptemanager = ptemanager;
            this.elem = elem;
            InitializeComponent();
        }

        private void ElementForm_Load(object sender, EventArgs e)
        {
            Owner.Move += owner_Move;
            owner_Move(null, null);
            lblElement.Text = String.Format("{0}\n{1}", elem.Number, elem.Symbol);
            lblNumber.Text = String.Format("[{0}]", elem.Number);
            lblSymbol.Text = elem.Symbol;
            lblName.Text = elem.Name;
            lblGroup.Text = String.Format("{0} - G {1} | P {2}", elem.GroupName, elem.GroupNumber, elem.PeriodNumber);
            lblDescription.Text = elem.Description;
            addTableInfo(0, elem.AtomicMass.ToString());
            addTableInfo(1, elem.State.ToString());
            addTableInfo(2, String.Join(", ", elem.OxidationNumbers));
            if (!double.IsNaN(elem.Electronegativity)) addTableInfo(3, elem.Electronegativity.ToString() + " (Pauling)");
            if (!double.IsNaN(elem.MeltingPoint)) addTableInfo(4, elem.MeltingPoint.ToString() + "°C");
            if (!double.IsNaN(elem.BoilingPoint)) addTableInfo(5, elem.BoilingPoint.ToString() + "°C");
            addTableInfo(6, elem.Configuration);
            if (elem.State == ElementState.Solid)
            { lblState.Text = "s"; lblState.BackColor = Color.Gray; }
            else if (elem.State == ElementState.Liquid)
            { lblState.Text = "l";  lblState.BackColor = Color.DodgerBlue; }
            else if (elem.State == ElementState.Gas)
            { lblState.Text = "g"; lblState.BackColor = Color.YellowGreen; }
            lblGroup.BackColor = ptemanager.GetCurrentColor(elem);
            panBox.BackColor = ptemanager.GetCurrentColor(elem);
            if (elem.Attribute == ElementType.Radioactive || elem.Attribute == ElementType.Synthetic) panRadioactive.Visible = true; 
        }

        private void addTableInfo(int row, string text)
        {
            tableInfo.Controls.Add(
                new Label() { Dock = DockStyle.Fill, Size = new Size(130, 21), TextAlign = ContentAlignment.MiddleCenter, Text = text },
                1, row);
        }

        private void owner_Move(object sender, EventArgs e)
        {
            int xpoint = Owner.Location.X + Owner.Width / 2 - this.Width / 2;
            int ypoint = Owner.Location.Y + Owner.Height / 2 - this.Height / 2;
            this.Location = new Point(xpoint, ypoint);
        }

        private void butClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void ElementForm_FormClosing(object sender, FormClosingEventArgs e)
        {
            Owner.Move -= owner_Move;
            this.Dispose();
        }
    }
}
